package com.smt.modules.job.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.smt.common.utils.Constant;
import com.smt.modules.bus.entity.BillEntity;
import com.smt.modules.bus.entity.LevelTaskEntity;
import com.smt.modules.bus.entity.MemberUserEntity;
import com.smt.modules.bus.service.*;
import com.smt.modules.job.service.CronRewardService;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 每天定时更新奖励
 *
 * @author zhangyi
 * @date 2018/10/27 15:28
 */
@Service
@Slf4j
public class CronRewardServiceImpl implements CronRewardService {
    @Autowired
    private MemberUserService memberUserService;

    @Autowired
    private LevelTaskService levelTaskService;
    @Autowired
    private BillService billService;

    /**
     * 定时更新奖励任务
     */
    @Scheduled(cron = "0 0 0 * * ?")
    @Override
    public void editUserStatus() {
        List<MemberUserEntity> memberUserEntities = memberUserService.selectList(new EntityWrapper<>());
        List<LevelTaskEntity> levelList = levelTaskService.selectList(new EntityWrapper<LevelTaskEntity>()
                .eq("type", 0));
        memberUserEntities.stream()
                .forEach(m -> {
                    levelList.stream().forEach(l -> {
                        BillEntity billEntity = new BillEntity(l.getTitle(),
                                1,
                                DateTime.now().toDate(),
                                m.getUsername(),
                                m.getUserId(),
                                DateTime.now().toDate(),
                                l.getPointValue(),
                                0);

                        if(Constant.DAY_1.equals(l.getTitle())){
                            billEntity.setIsReward(1);
                        }else{
                            billEntity.setIsReward(0);
                        }
                        boolean insert = billService.insert(billEntity);
                        log.info("用户：{} 每日奖励账单插入状态 {}",m.getUserId(),insert);
                    });
                });
    }
}
